﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PlayBox.DataClasses;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Windows.Forms;

namespace PlayBox
{
    class FileSystem
    {
        private const string _autoLoginPath = "HDD\\autologin.txt";

        public UserData LastUser
        {
            get
            {
                // Gibts kein Autologin?
                if (!System.IO.File.Exists(_autoLoginPath))
                    return null;

                try
                {
                    // Usernamen holen
                    string username = System.IO.File.ReadAllText(_autoLoginPath);

                    // Userdata holen
                    return GetUserData(username);
                }
                catch
                {
                    return null;
                }
            }
            set
            {
                // -----------------------------------
                // Auto Login entfernen
                // -----------------------------------
                if (value == null)
                {
                    if (System.IO.File.Exists(_autoLoginPath))
                    {
                        try
                        {
                            System.IO.File.Delete(_autoLoginPath);
                        }
                        catch
                        {

                        }
                    }
                }
                else
                {
                    // -----------------------------------
                    // Benutzernamen speichern
                    // -----------------------------------

                    try
                    {
                        System.IO.File.WriteAllText(_autoLoginPath, value.Username);
                    }
                    catch
                    {

                    }
                }
            }
        }


        public UserData GetUserData(String username)
        {
            // Existiert Benutzer?
            string filepath = UserData.GetDirectoryPath(username, "Profile\\data.txt");
            if (System.IO.File.Exists(filepath))
            {
                EncryptionLayer enc = null;
                try
                {
                    enc = new EncryptionLayer(filepath, username, true);
                    UserData userdata = (UserData) enc.Deserialize();

                    return userdata;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Fehler beim Laden der Benutzerdaten.\n" + ex.Message, "Fehler", MessageBoxButtons.OK);
                }
                finally
                {
                    if (enc != null)
                        enc.close();
                }
            }



            /*if (username == "test")
            {
                var data = new PlayBox.DataClasses.UserData();
                data.Username = username;
                data.SetPassword("test");
                return data;
            }*/
            return null;
        }

        public bool SetUserData(UserData userData)
        {
            EncryptionLayer enc = null;
            try
            {
                System.IO.Directory.CreateDirectory(userData.GetDirectoryPath("Profile"));

                enc = new EncryptionLayer(userData.GetDirectoryPath("Profile\\data.txt"), userData.Username, false);
                enc.Serialize(userData);

                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fehler beim Speichern der Benutzerdaten.\n" + ex.Message, "Fehler", MessageBoxButtons.OK);
            }
            finally
            {
                if (enc != null)
                    enc.close();
            }

            return false;
        }
    }
}
